package com.hyfd.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class WangSenFlowTask
{
    
    Logger log = Logger.getLogger(getClass());
    
    @Autowired
    OrderDao orderDao;
    
    @Autowired
    RabbitMqProducer mqProducer;// 消息队列生产者
    
    @Autowired
    ProviderPhysicalChannelDao providerPhysicalChannelDao;// 物理通道信息
    
//     @Scheduled(fixedDelay = 60000)
    public void queryWangSenOrder()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = 2;
        try
        {
            String providerId = "0000000085";//
            Map<String, Object> channel = providerPhysicalChannelDao.selectByProviderId(providerId);// 获取通道
            String defaultParameter = (String)channel.get("default_parameter");// 参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String linkUrl = (String)channel.get("link_url");// 充值地址
            String Action = "getOrder";// 操作（固定值Charge）
            String V = "1.1";
            String Account = paramMap.get("Account");// 平台申请的独立API账号
            String apiKey = paramMap.get("apiKey");// 平台的appKey
            // 查询订单中属于指通的折扣
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("dispatcherProviderId", providerId);
            param.put("status", "1");
            List<Map<String, Object>> orderList = orderDao.selectByTask(param);// 查询出所有尚通的充值中的订单
            for (int i = 0, size = orderList.size(); i < size; i++)
            {
                Map<String, Object> order = orderList.get(i);
                String orderId = order.get("orderId") + "";// 平台订单号
                
                map.put("orderId", orderId);
                String md5Str = "account=" + Account +"&outtradeno="+orderId+ "&key=" + apiKey;
                String sign = MD5.ToMD5(md5Str);
                String paramStr =
                    "V=" + V + "&Action=" + Action + "&Account=" + Account + "&OutTradeNo=" + orderId + "&Sign=" + sign;
                String result = ToolHttp.post(false, linkUrl, paramStr, "application/x-www-form-urlencoded");
                // 判断是否成功
                if (result != null && !result.equals(""))
                {
                    JSONObject resultJson = JSONObject.parseObject(result);// 将返回值转为json对象
                    String code = resultJson.getString("Code");// 返回状态码
                    String message = resultJson.getString("Message");// 返回信息
                    
                    if (code.equals("0"))
                    {
                        JSONObject reports = resultJson.getJSONObject("Data");
                        String status = reports.getString("Status");// 充值状态
                        // String TaskID = reports.getString("TaskID");
                        map.put("resultCode", status + ":" + message);
                        if (status.equals("4"))
                        {
                            flag = 1;
                        }
                        else if (status.equals("5"))
                        {
                            flag = 0;
                        }
                        else if (status.equals("6"))
                        {
                            continue;
                        }
                    }
                }
                map.put("status", flag);
                mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
            }
        }
        catch (Exception e)
        {
            log.error("指通流量查询出错" + e);
        }
    }
}
